﻿using IOSendInterface;
using PipeServer.Enum;

namespace PipeServer.Model
{
        /// <summary>
        /// 数据包
        /// </summary>
        internal class DataPage
        {

                /// <summary>
                /// 数据内容
                /// </summary>
                public byte[] Content;
                /// <summary>
                /// 数据包的唯一标示
                /// </summary>
                public int DataId;

                /// <summary>
                /// 数据的长度
                /// </summary>
                public int DataLen;

                /// <summary>
                /// 数据包的类型
                /// </summary>
                public Enum.PageType PageType;


                /// <summary>
                /// 数据类型（1 字符串类型 , 2 实体对象 3 字节流）
                /// </summary>
                public SendType DataType;



                /// <summary>
                /// 包的类型(用户自定义)
                /// </summary>
                public char[] Type;
                public byte HeadType;

                private static byte[] _GetSendStream (Page page)
                {
                        if (page.SendData == null)
                        {
                                return System.Array.Empty<byte>();
                        }
                        else if (page.SendType == SendType.字节流)
                        {
                                return (byte[])page.SendData;
                        }
                        else if (page.SendType == SendType.字符串)
                        {
                                return ToolsHelper.SerializationString (page.SendData.ToString ());
                        }
                        else
                        {
                                return ToolsHelper.SerializationClass (page.SendData);
                        }
                }
                /// <summary>
                /// 获取数据包的信息
                /// </summary>
                /// <returns></returns>
                public static DataPage GetDataPage (Page page)
                {
                        DataPage temp = new DataPage
                        {
                                DataId = page.PageId
                        };
                        byte[] content = _GetSendStream (page);
                        temp.Content = content;
                        temp.DataLen = content.Length;
                        temp.DataType = page.SendType;
                        temp.PageType = page.PageType;
                        temp.Type = page.Type.ToCharArray ();
                        return temp;
                }
                public int GetPageSize ()
                {
                        if (this.DataLen == 0)
                        {
                                return this.Type.Length + PipeHelper.HeadLen + 1;
                        }
                        else if (this.DataLen <= short.MaxValue)
                        {
                                this.HeadType = 2;
                                return this.Type.Length + 3 + PipeHelper.HeadLen + this.DataLen;
                        }
                        else
                        {
                                this.HeadType = 4;
                                return this.Type.Length + 5 + PipeHelper.HeadLen + this.DataLen;
                        }
                }
        }
}
